using DTFramework.UI;
using GameFramework;
using System;
using System.Reflection;
using UnityEngine;
using UnityGameFramework.Runtime;

namespace DTFramework
{
    public class BuiltinDataComponent : GameFrameworkComponent
    {
        [SerializeField] BuiltinConfig m_BuiltinConfigAsset;

        private UILoadingForm _loadingFormTemp;
        private NativeDialogForm _nativeDialogTemp;
        private UILoginForm _uiLoginForm;
        private UICheckRunEnvirForm _uICheckRunEnvirFormTemp;

        public BuildInfo BuildInfo { get; set; }
        public BuiltinConfig BuiltinConfig { get => m_BuiltinConfigAsset; set => m_BuiltinConfigAsset = value; }
        protected override void Awake()
        {
            base.Awake();
            if (m_BuiltinConfigAsset == null)
            {
                Debug.LogError("请创建并关联一个 BuiltinConfig.asset ！！");
            }
        }
        public void InitBuildInfo()
        {
            if (m_BuiltinConfigAsset.m_BuildInfoTextAsset == null || string.IsNullOrEmpty(m_BuiltinConfigAsset.m_BuildInfoTextAsset.text))
            {
                Debug.Log("Build info can not be found or empty.");
                return;
            }

            BuildInfo = Utility.Json.ToObject<BuildInfo>(m_BuiltinConfigAsset.m_BuildInfoTextAsset.text);
            if (BuildInfo == null)
            {
                Debug.LogError("Parse build info failure.");
                return;
            }
        }

        #region 常用 UI 配置 
        /// <summary>
        /// 登录
        /// </summary> 
        public void OpenLoginForm(string url, Action<bool, string> OnLoginResult)
        {
            _uiLoginForm = Instantiate(m_BuiltinConfigAsset.m_UILoginFormTemplate);
            _uiLoginForm.gameObject.SetActive(true);

            _uiLoginForm.Init(url);
            _uiLoginForm.OnLoginResult = OnLoginResult;
        }
        /// <summary>
        /// 关闭登录ＵＩ
        /// </summary>
        public void CloseLoginForm()
        {
            if (_uiLoginForm != null)
            {
                Destroy(_uiLoginForm.gameObject, 0.1f);
            }
            _uiLoginForm = null;
        }

        /// <summary>
        /// 设置进度
        /// </summary>
        public UILoadingForm StartProgress()
        {
            if (_loadingFormTemp == null)
            {
                _loadingFormTemp = Instantiate(m_BuiltinConfigAsset.m_UILoadingFormTemplate);
                _loadingFormTemp.gameObject.SetActive(true);
                return _loadingFormTemp;
            }
            return null;
        }
        public void UpdateProgress(float value, string content = null)
            => _loadingFormTemp?.SetProgress(value, content);
        public void OverProgress()
        {
            if (_loadingFormTemp != null)
            {
                _loadingFormTemp.gameObject.SetActive(false);
                Destroy(_loadingFormTemp.gameObject);
            }
            _loadingFormTemp = null;
        }

        /// <summary>
        /// 打开对话框
        /// </summary>
        public void OpenDialogForm(DialogParams dialogParams)
        {
            if (_nativeDialogTemp == null)
            {
                _nativeDialogTemp = Instantiate(m_BuiltinConfigAsset.m_NativeDialogFormTemplate);
                _nativeDialogTemp.gameObject.SetActive(true);
            }
            _nativeDialogTemp.OnOpen(dialogParams);
        }
        /// <summary>
        /// 结束对话框
        /// </summary>
        public void OverDialogForm()
        {
            if (_nativeDialogTemp != null)
            {
                Destroy(_nativeDialogTemp.gameObject, 0.1f);
            }
            _nativeDialogTemp = null;
        }

        /// <summary>
        /// 打开互联网检测ＵＩ
        /// </summary>
        /// <param name="dialogParams"></param>
        public void OpenCheckRunEnvirForm(string content)
        {
            if (_uICheckRunEnvirFormTemp == null)
            {
                _uICheckRunEnvirFormTemp = Instantiate(m_BuiltinConfigAsset.m_UICheckRunEnvirFormTemplate);
                _uICheckRunEnvirFormTemp.gameObject.SetActive(true);
                _uICheckRunEnvirFormTemp.SetText(content);
            }
            else
            {
                _uICheckRunEnvirFormTemp.gameObject.SetActive(true);
                _uICheckRunEnvirFormTemp.SetText(content);
            }
        }
        /// <summary>
        /// 关闭互联网检测ＵＩ
        /// </summary>
        public void CloseCheckRunEnvirForm()
        {
            if (_uICheckRunEnvirFormTemp != null)
            {
                Destroy(_uICheckRunEnvirFormTemp.gameObject, 0.1f);
            }
            _uICheckRunEnvirFormTemp = null;
        }
        #endregion


        public Type GetType(string name)
        {
            if (m_BuiltinConfigAsset.m_ProjectAssembly == null)
            {
                return null;
            }
            var assembly = Assembly.Load(m_BuiltinConfigAsset.m_ProjectAssembly);
            return assembly.GetType(name);
        }
        public Type GetProcedureEntry() => GetType(m_BuiltinConfigAsset.m_ProjectProcedureEntry);
    }
}
